package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TransferRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Wallet;
import com.ruoyi.system.service.IWalletService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 钱包Controller
 * 
 * @author ruoyi
 * @date 2025-01-04
 */
@RestController
@RequestMapping("/system/wallet")
public class WalletController extends BaseController
{
    @Autowired
    private IWalletService walletService;

    /**
     * 查询钱包列表
     */
    @PreAuthorize("@ss.hasPermi('system:wallet:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wallet wallet)
    {
        startPage();
        List<Wallet> list = walletService.selectWalletList(wallet);
        return getDataTable(list);
    }

    /**
     * 导出钱包列表
     */
    @PreAuthorize("@ss.hasPermi('system:wallet:export')")
    @Log(title = "钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wallet wallet)
    {
        List<Wallet> list = walletService.selectWalletList(wallet);
        ExcelUtil<Wallet> util = new ExcelUtil<Wallet>(Wallet.class);
        util.exportExcel(response, list, "钱包数据");
    }

    /**
     * 获取钱包详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:wallet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(walletService.selectWalletById(id));
    }

    /**
     * 新增钱包
     */
//    @PreAuthorize("@ss.hasPermi('system:wallet:add')")
    @Log(title = "钱包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Long userId)
    {
        return toAjax(walletService.insertWallet(userId));
    }

    /**
     * 钱包转账 货物订单使用
     */
//    @PreAuthorize("@ss.hasPermi('system:wallet:edit')")
    @Log(title = "钱包", businessType = BusinessType.UPDATE)
    @PutMapping("/transfer")
    public AjaxResult edit(@RequestBody TransferRequest request) throws InterruptedException {
        List<Long> longList = walletService.transferWallet(request);
        int i =0;
        if (longList.size() > 0) {
            i = 1;
        }
        return toAjax(i);
    }

    /**
     * 钱包转账
     *      钱包转入转出 里程表提现 商城
     */
//    @PreAuthorize("@ss.hasPermi('system:wallet:edit')")
    @Log(title = "钱包", businessType = BusinessType.UPDATE)
    @PutMapping("/transaction")
    public AjaxResult transaction(@RequestBody TransferRequest request) throws InterruptedException {

        return toAjax(walletService.WalletTransaction(request));
    }
//    @PreAuthorize("@ss.hasPermi('system:wallet:edit')")
    @Log(title = "钱包", businessType = BusinessType.UPDATE)
    @PutMapping("/goods")
    public AjaxResult goods(@RequestBody TransferRequest request) throws InterruptedException {

        return toAjax(walletService.goodsPay(request));
    }
    /**
     * 修改钱包goodsPay
     */
    @PreAuthorize("@ss.hasPermi('system:wallet:edit')")
    @Log(title = "钱包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wallet wallet)
    {
        return toAjax(walletService.updateWallet(wallet));
    }

    /**
     * 删除钱包
     */
    @PreAuthorize("@ss.hasPermi('system:wallet:remove')")
    @Log(title = "钱包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(walletService.deleteWalletByIds(ids));
    }
}
